/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lojavirtual.persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;
import lojavirtual.model.Categoria;
import lojavirtual.util.ConnectionFactory;
import lojavirtual.util.HibernateUtil;
import org.hibernate.Session;

/**
 *
 * @author 710930
 */
public class CategoriaDAO implements DAO<Categoria> {

    public void inserir(Categoria categoria) {
       Connection connection = ConnectionFactory.getConnection();

       try{

           String SQL = " INSERT INTO CATEGORIA(NOME, DESCRICAO) VALUES (?, ?) ";
           PreparedStatement statement = connection.prepareStatement(SQL);
           statement.setString(1, categoria.getNome());
           statement.setString(2, categoria.getDescricao());
           statement.executeUpdate();

       } catch (Exception e){ e.printStackTrace();}
       finally { ConnectionFactory.close(connection); }
    }

    public void alterar(Categoria categoria) {
           Connection connection = ConnectionFactory.getConnection();

           try{

               String SQL = "UPDATE CATEGORIA (NOME, DESCRICAO) SET (?, ?) WHERE ID = ?";

               PreparedStatement statement = connection.prepareStatement(SQL);
               statement.setString(1, categoria.getNome());
               statement.setString(2, categoria.getDescricao());
               statement.setInt(3, categoria.getId());
               statement.executeUpdate();

           } catch (Exception e){ e.printStackTrace();}
           finally { ConnectionFactory.close(connection); }
    }

    public List listar(Categoria categoria) {
       Connection connection = ConnectionFactory.getConnection();
       
       try{

           String SQL = "DELETE FROM CATEGORIA WHERE ID = ?";

           PreparedStatement statement = connection.prepareStatement(SQL);
           statement.setInt(1, categoria.getId());
           statement.executeUpdate();

       } catch (Exception e){ e.printStackTrace();}
       finally { ConnectionFactory.close(connection); }

       return null;
    }

    public void remover(Categoria categoria) {
       Connection connection = ConnectionFactory.getConnection();

       try{

           String SQL = "DELETE FROM CATEGORIA WHERE ID = ?";

           PreparedStatement statement = connection.prepareStatement(SQL);
           statement.setInt(1, categoria.getId());
           statement.executeUpdate();

       } catch (Exception e){ e.printStackTrace();}
       finally { ConnectionFactory.close(connection); }
    }

    public List<Categoria> listarCategorias(){
        Session session = HibernateUtil.getSessionFactory().openSession();        
        List<Categoria> categorias = session.createCriteria(Categoria.class).list();
        session.close();
        return categorias;
    }

    public static void main(String[] args){
        System.out.println(new CategoriaDAO().listarCategorias());
    }

}
